import { Component, OnInit, ChangeDetectorRef, forwardRef, Inject } from '@angular/core';
import { Router } from '@angular/router';
import { path } from '../../../path.config';
import { masterService } from '../../../../lib/master.service';
import { AppComponent } from '../../../app.component';
@Component({
    selector: 'app-change-password',
    templateUrl: './change-password.component.html',
    styleUrls: ['./change-password.component.css']
})
export class ChangePasswordComponent implements OnInit {
    private changePassWord = path.changePassWord.update;
    private startTime: number = 6;
    public msgInfo: any;
    public msgType = false;
    public changeModel = {
        oldPassword: '',
        newPassword: ''
    };

    public againPassWord = '';
    public userName = '';

    constructor(private _masterService: masterService,
        private _router: Router,
        @Inject(forwardRef(() => AppComponent)) private app: AppComponent,
        private cdRef: ChangeDetectorRef) { }

    ngOnInit() { }
    // 修改密码提交  
    submit() {
        if (!this.changeModel.oldPassword) {
            this.msgInfo = '请填写原密码';
            this.msgType = false;
            return;
        } else if (!this.changeModel.newPassword) {
            this.msgInfo = '请填写新密码';
            this.msgType = false;
            return;
        } else if (!this.againPassWord) {
            this.msgInfo = '请再次输入密码';
            this.msgType = false;
            return;
        } else if (this.againPassWord !== this.changeModel.newPassword) {
            this.msgInfo = '两次输入密码不一致';
            this.msgType = false;
            return;
        }
        this.msgInfo = '';
        this._masterService
            .postData(this.changePassWord, this.changeModel)
            .subscribe((res) => {
                this.msgInfo = '';
                if (res.statusCode == 200) {
                    this.msgType = true;
                    this.cdRef.detectChanges();
                } else if (res.statusCode == 499 || res.statusCode == 401) {
                    this.msgInfo = '账户或者密码错误!';
                    this.msgType = false;
                    this.cdRef.detectChanges();
                } else if (res.statusCode == 11000109) {
                    this.msgInfo = '原密码错误!'
                    this.msgType = false;
                    this.cdRef.detectChanges();
                }
            })
    }
}
